﻿@inject IHttpService _httpService
@inject IPopupService PopupService


@if (Model == null)
{
    <MDCard>
        <MCardText Class="pt-8 pb-8">
            <CnGalWebSite.Components.Progressings.ProgressingCard />
        </MCardText>
    </MDCard>
}
else
{
    <MDCard>
        <MCardTitle>
            @(Id == 0 ? $"添加{_name}" : $"编辑{_name}")  @Model.Name
        </MCardTitle>
        <MCardText>
            Id：@Model.Id
        </MCardText>
        <MCardText>
            <MRow>
                <MCol Cols="12" Md="6">
                    <CnGalWebSite.Components.Inputs.MasaSelecte @bind-Value="Model.Type" TValue="TimedTaskType" Label="任务类型" />
                </MCol>
                <MCol Cols="12" Md="6">
                    <CnGalWebSite.Components.Inputs.MasaSelecte @bind-Value="Model.ExecuteType" TValue="TimedTaskExecuteType" Label="定时类型" />
                </MCol>
                @if (Model.ExecuteType == TimedTaskExecuteType.EveryDay)
                {
                    <MCol Cols="12" Md="6">
                        <CnGalWebSite.Components.Inputs.MasaDatePicker @bind-NullableValue="Model.EveryTime" Nullable ShowDate="false" ShowTime Label="固定时间" />
                     </MCol>
                }
                else
                {
                    <MCol Cols="12" Md="6">
                        <MTextField @bind-Value="Model.IntervalTime" Label="间隔时间（分钟）" />
                    </MCol>
                }
                  <MCol Cols="12"></MCol>
                <MCol Cols="12" Xl="4">
                    <MCheckbox @bind-Value="Model.IsLastFail" Label="上次执行失败" TValue="bool" />
                </MCol>
                <MCol Cols="12" Xl="4">
                    <MCheckbox @bind-Value="Model.IsPause" Label="暂停" TValue="bool" />
                </MCol>
                <MCol Cols="12" Xl="4">
                    <MCheckbox @bind-Value="Model.IsRuning" Label="正在执行" TValue="bool" />
                </MCol>
            </MRow>

        </MCardText>
        <MCardActions Class="pb-6 ps-6 pe-6">
            <MSpacer></MSpacer>
            <MButton Color="primary"
                  Text
                      OnClick="SaveEdit">
                 <MIcon Left>mdi-check</MIcon>
             保存
         </MButton>
     </MCardActions>

 </MDCard>

}



@code {
    [Parameter]
    public int Id { get; set; }
    [Parameter]
    public EventCallback OnEdited { get; set; }

    TimedTaskEditModel Model;

    private string _baseUrl = ToolHelper.TaskApiPath + "api/timedtasks/";
    private string _name = "定时任务";


    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await Refresh();
    }

    public async Task Refresh()
    {
        try
        {
            Model = null;
            StateHasChanged();
            Model = Id == 0 ? new TimedTaskEditModel() : await _httpService.GetAsync<TimedTaskEditModel>($"{_baseUrl}edit?id={Id}");
            StateHasChanged();
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, $"获取{_name}信息失败");
        }
    }

    public async Task SaveEdit()
    {
        try
        {
            var result = await _httpService.PostAsync<TimedTaskEditModel,CnGalWebSite.Core.Models.Result>($"{_baseUrl}edit", Model);
            if (result.Success)
            {
                if (!string.IsNullOrWhiteSpace(result.Message))
                {
                    await PopupService.ToastAsync(result.Message, AlertTypes.Success);
                }
                await OnEdited.InvokeAsync(Model);
                StateHasChanged();
            }
            else
            {
                await PopupService.ToastAsync(result.Message ?? "操作失败", AlertTypes.Error);
            }
        }
        catch (Exception ex)
        {
            await ErrorHandler.ProcessError(ex, Model.Id == 0 ? $"添加{_name}失败" : $"编辑{_name}信息失败");
        }

    }
}
